<?php

namespace App\Admin\Controllers;

use App\Coupon;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CouponController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('优惠券列表');
            $content->description('优惠券列表');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('优惠券修改');
            $content->description('优惠券修改');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('优惠券增加');
            $content->description('优惠券增加');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Coupon::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->name('优惠券名称');
            $grid->money('优惠金额');
            $grid->start_time('开始时间');
            $grid->end_time('结束时间');
            $grid->disableExport();
            $grid->disableFilter();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Coupon::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('name','优惠券名称')->placeholder('输入优惠券名称')->rules(
                'required',
                ['required'=>'优惠券名称不能为空']
            );
            $form->select('type','优惠券类型')->options(Coupon::$typeMap);
            $form->currency('money','优惠金额')->symbol('元')->rules('required',[
                'required'=>'请填写优惠金额'
            ]);
            $form->datetimeRange('start_time', 'end_time', '有效时间段');
        });
    }
}
